package com.apes.fn.scm.merchant.account.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.generator.Implementation;
import com.apes.framework.jpa.tuple.date.DateTime;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.JpaUtil;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.finance.model.Bank;
import com.apes.scm.masterdata.finance.model.BankAccount;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.service.PartyRoleService;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.Objects;

/**
 * @desc 定义银行账户
 *
 * @author muting
 * @create 2018年8月9日15:16:31
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = "merchant_bank_account")
public class MerchantBankAccount extends Model {
    public static final String CONST_TABLE_NAME = "merchant_bank_account";

    @Id
    @Comment("账号编码")
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "账号编码", padding = 4, implementation = Implementation.NO_GAP)
    private String id;

    @Name
    @Comment("户名")
    @Column(name = "name", nullable = false, length = 64)
    private String name;

    @ManyToOne(optional = false)
    @JoinColumn(name = "merchant_settlement_id", referencedColumnName = "id")
    @Comment("赋能商户结算对账单")
    private MerchantAccountSettlement merchantSettlement;

    @Comment("银行")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bank_id", referencedColumnName = "id")
    private Bank bank;

    @Name
    @Comment("银行账户")
    @Column(name = "bank_Account_id", length = 120, nullable = false)
    private String bankAccountId;

    @Name
    @Comment("银行账户ID")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "account_id", referencedColumnName = "id")
    private BankAccount accountId;

    @Comment("收款金额")
    @Column(name = "amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double amount = 0;

    @Column(name = "remark", length = 300)
    @Comment("备注")
    private String remark;

    @Column(name = "disbursement_already_amount", columnDefinition = ApesConst.FIELD_16_4)
    @Comment("已排款金额")
    private double disbursementAlreadyAmount = 0;

    @Column(name = "pay_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("付款金额")
    private double payAmount = 0;

    @Column(name = "pay_date")
    @Comment("付款时间")
    private Date payDate;

    /**
     * 银行
     **/
    @Transient
    private Bank standbyBank;


    public Bank getStandbyBank() {
        if (Objects.isNull(this.accountId)) return null;
        return this.accountId.getBank();
    }

    /**
     * 收入类别
     **/
    @Transient
    private Enumeration standbyType;


    public Enumeration getStandbyType() {
        if (Objects.isNull(this.accountId)) return null;
        return this.accountId.getType();
    }

    /**
     * 银行账户
     **/
    @Transient
    private String standbyBankAccountId;


    public String getStandbyBankAccountId() {
        if (Objects.isNull(this.accountId)) return null;
        return this.accountId.getAccountId();
    }

    /**
     * 户名
     **/
    @Transient
    private String standbyName;


    public String getStandbyName() {
        if (Objects.isNull(this.accountId)) return null;
        return this.accountId.getAccountName();
    }


}
